package com.zhijiang.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.zhijiang.hospital.entity.dto.OutpatientDepartmentDto;
import com.zhijiang.hospital.entity.dto.OutpatientsRunDto;
import com.zhijiang.hospital.service.OutpatientDepartmentService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.PageList;
import com.zhijiang.util.Result;
import com.zhijiang.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital-data
 * @description
 * @date 2022-10-25 20:53:42
 */
@Api(tags = "门诊运行的数据")
@RestController
@RequestMapping("Outpatient")
//@PreAuthorize("hasAnyAuthority('admin','test','system:dept:test')")
public class OutpatientDepartmentController {
    @Autowired
    OutpatientDepartmentService outpatientDepartmentService;
    //@Autowired
    //private KeyValueRepository keyValueRepository;
    //@Autowired
    //private QuerydslKeyValueRepository querydslKeyValueRepository;

    @ApiOperation("门诊运行Excel导出数据接口")
    @ApiModelProperty(value = "门诊运行Excel导出数据接口")
    @GetMapping("loadExcel")
    public void loadExcel(HttpServletResponse response) throws IOException {
        List<OutpatientDepartmentDto> list = outpatientDepartmentService.findAll();
        String title = "门诊运行的数据";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("门诊运行的数据.xlsx", "utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), OutpatientDepartmentDto.class).sheet(title).doWrite(list);
    }

    //当天
    @ApiOperation("门诊运行的数据分页")
    @ApiModelProperty(value = "门诊运行的数据 ")
    @PostMapping("OutpatientsRun")
    public Result OutpatientToDay(@RequestBody BaseQuery baseQuery) {
        BaseQuery query = TimeUtil.initBaseQuery(baseQuery);
        Map<String, Object> outpatientsRunBooking = outpatientDepartmentService.getOutpatientsRunBooking(query);
        PageList pageList = outpatientDepartmentService.getOutpatientsRunBookingByPageList(query);
        outpatientsRunBooking.put("jihe", pageList);
        return Result.ok(outpatientsRunBooking);
    }

    @ApiOperation("今年年度门诊运行的数据")
    @ApiModelProperty("今年年度门诊运行的数据分页")
    @GetMapping("/getYearData")
    public Result getYearData() {
        List<OutpatientsRunDto> yearData = outpatientDepartmentService.getYearData();
        return Result.ok(yearData);
    }

    @ApiOperation("去年年度门诊运行的数据")
    @ApiModelProperty("去年年度门诊运行的数据分页")
    @GetMapping("/getLastYearData")
    public Result getLastYearData() {
        List<OutpatientsRunDto> lastYearData = outpatientDepartmentService.getLastYearData();
        return Result.ok(lastYearData);
    }


}
